package com.sunrise.dispatcher.addressbook;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class LoginWindow extends Window {
	private Button btnLogin = new Button("Login");
	private TextField login = new TextField("Username");
	private PasswordField password = new PasswordField("Password");

	public LoginWindow() {
		super("Authentication Required !");
		setName("login");
		initUI();
	}

	private void initUI() {

		addComponent(new Label("Please login in order to use the application"));
		addComponent(new Label());
		addComponent(login);
		addComponent(password);
		addComponent(btnLogin);

		btnLogin.addListener(new Button.ClickListener() {
			public void buttonClick(Button.ClickEvent event) {
				try {
					AddressBookApplication.getInstance().authenticate((String) login.getValue(),
							(String) password.getValue());
					open(new ExternalResource(AddressBookApplication.getInstance().getURL()));
				} catch (Exception e) {
					showNotification(e.toString());
				}
			}
		});
	}
}